package tree.binarytree2;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BinaryTree implements IBinaryTree {


    @Override
    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        return A;
    }

    @Override
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    @Override
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        preOrder(root.left);
        System.out.print(root.val + " ");
        preOrder(root.right);
    }

    @Override
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }

    public int size = 0;

    @Override
    public void size(TreeNode root) {
        if (root == null) {
            return;
        }
        size++;
        size(root.left);
        size(root.right);
    }

    @Override
    public int size2(TreeNode root) {
        return root == null ? 0 : size2(root.left) + size2(root.right) + 1;
    }

    public int getLeafCount = 0;

    @Override
    public void getLeafNodeCount1(TreeNode root) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            getLeafCount++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

    @Override
    public int getLeafNodeCount2(TreeNode root) {
        return root.left == null && root.right == null ?
                1 : getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    @Override
    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null || k < 1 || k > getHeight(root)) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }

    @Override
    public int getHeight(TreeNode root) {
        return root == null ? 0 : Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }

    @Override
    public TreeNode find(TreeNode root, char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode left = find(root.left, val);
        if (left != null) {
            return left;
        }
        return find(root.right, val);
    }

    @Override
    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

    @Override
    public boolean isCompleteTree(TreeNode root) {
        if (root == null) {
            return true;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        boolean flag = false;
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            if (cur == null) {
                flag = true;
            } else {
                if (flag) {
                    return false;
                }
                queue.offer(cur.left);
                queue.offer(cur.right);
            }
        }
        return true;
    }

    private boolean getPath(TreeNode root, TreeNode target, Stack<TreeNode> s) {
        if (root == null) {
            return false;
        }
        s.push(root);
        if (root == target) {
            return true;
        }
        boolean ret = getPath(root.left, target, s);
        if (ret) {
            return true;
        }
        s.pop();
        return false;
    }

    public void preOrderNor(TreeNode root) {
        if (root == null) return;
        Stack<TreeNode> s = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !s.isEmpty()) {
            while (cur != null) {
                s.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = s.pop();
            cur = top.right;
        }
    }

    public void inOrderNor(TreeNode root) {
        if (root == null) return;
        Stack<TreeNode> s = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !s.isEmpty()) {
            while (cur != null) {
                s.push(cur);
                cur = cur.left;
            }
            TreeNode top = s.pop();
            System.out.print(top.val + " ");
            cur = top.right;
        }
    }

    public void postOrderNor(TreeNode root) {
        if (root == null) return;
        Stack<TreeNode> s = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null || !s.isEmpty()) {
            while (cur != null) {
                s.push(cur);
                cur = cur.left;
            }
            TreeNode top = s.peek();
            if (top.right == null || top.right == prev) {
                System.out.print(top.val + " ");
                s.pop();
                prev = top;
            } else {
                cur = top.right;
            }
        }
    }

}
